package com.hx.resource.controller;

import com.hx.common.annotation.Log;
import com.hx.common.core.controller.BaseController;
import com.hx.common.core.domain.AjaxResult;
import com.hx.common.core.page.TableDataInfo;
import com.hx.common.enums.BusinessType;
import com.hx.common.utils.poi.ExcelUtil;
import com.hx.resource.domain.FileInfo;
import com.hx.resource.domain.FileShare;
import com.hx.resource.service.IFileInfoService;
import com.hx.resource.service.IFileShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 文件Controller
 * 
 * @author mohao
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/info")
public class FileInfoController extends BaseController
{
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private IFileShareService fileShareService;

    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('resource:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileInfo fileInfo)
    {
        List<FileInfo> list = fileInfoService.selectFileInfoList(fileInfo);
        return getDataTable(list);
    }

    /**
     * 模糊查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('resource:info:lists')")
    @GetMapping("/lists")
    public TableDataInfo querylist(FileInfo fileInfo)
    {
        System.out.println(fileInfo);
        List<FileInfo> list = fileInfoService.queryFileInfoList(fileInfo);
        return getDataTable(list);
    }

    /**
     * 导出文件列表
     */
    @PreAuthorize("@ss.hasPermi('resource:info:export')")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileInfo fileInfo)
    {
        List<FileInfo> list = fileInfoService.selectFileInfoList(fileInfo);
        ExcelUtil<FileInfo> util = new ExcelUtil<FileInfo>(FileInfo.class);
        util.exportExcel(response, list, "文件数据");
    }

    /**
     * 获取文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fileInfoService.selectFileInfoById(id));
    }

    /**
     * 新增文件
     */
    @PreAuthorize("@ss.hasPermi('resource:info:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileInfo fileInfo)
    {
        Long userId = getUserId();
        fileInfo.setCrateDate(new Date());
        fileInfo.setUserId(userId);
        fileInfo.setState(1);
        fileInfo.setFolder(0);
        System.out.println(fileInfo);
        return toAjax(fileInfoService.insertFileInfo(fileInfo));
    }

    /**
     * 修改文件
     */
    @PreAuthorize("@ss.hasPermi('resource:info:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody FileInfo fileInfo)
    {
        FileShare share = new FileShare();
        share.setFileId(fileInfo.getId());
        List<FileShare> list = fileShareService.selectFileShareList(share);
        for (FileShare fileShare : list) {
            fileShare.setPower((long)fileInfo.getState());
            fileShareService.updateFileShare(fileShare);
        }
        return toAjax(fileInfoService.updateFileInfo(fileInfo));
    }

    /**
     * 删除文件
     */
    @PreAuthorize("@ss.hasPermi('resource:info:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fileInfoService.deleteFileInfoByIds(ids));
    }
    /**
     * 添加文件夹
     */
    @PreAuthorize("@ss.hasPermi('resource:info:addFolder')")
    @Log(title = "文件夹", businessType = BusinessType.DELETE)
    @GetMapping("/addFolder")
    public AjaxResult addFolder(FileInfo  fileInfo)
    {
        Long userId = getUserId();
        fileInfo.setUserId(userId);
        fileInfo.setFolder(1);
        fileInfo.setFileType(3L);
        fileInfo.setCrateDate(new Date());
        fileInfo.setFileSize("0");
        fileInfo.setState(1);
        fileInfoService.insertFileInfo(fileInfo);
        return success("添加成功！");
    }
    @PreAuthorize("@ss.hasPermi('resource:info:folderList')")
    @Log(title = "文件夹", businessType = BusinessType.DELETE)
    @GetMapping("folderList")
    public TableDataInfo getFolder(FileInfo  fileInfo)
    {
        List<FileInfo> list = fileInfoService.selectFolderInfoList(fileInfo);
        return getDataTable(list);
    }
}
